package com.sam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sam.utils.Utils;

public class Logout extends ActionSupport {

	private static final long serialVersionUID = -1099903044658961437L;
	private String url;

	public String execute() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put("usuario", null);
		session.put("afiliadoValido", null);

		boolean googleLogin = Boolean.parseBoolean(Utils
				.getProperty("googleLogin"));

		if (googleLogin) {
			UserService userService = UserServiceFactory.getUserService();
			HttpServletRequest request = ServletActionContext.getRequest();
			if (request.getUserPrincipal() != null) {
				setUrl(userService.createLogoutURL("/login.action"));
				return "logout";
			}
		}
		return SUCCESS;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
